package com.lzb.musicplayer;

import android.graphics.Color;
import android.view.View;

import androidx.annotation.NonNull;

import com.lzb.musicplayer.audio.AudioPlayerManager;
import com.lzb.musicplayer.bean.MusicFile;
import com.lzb.musicplayer.databinding.ActivityHomeBinding;
import com.lzb.musicplayer.fragment.AlbumFragment;
import com.lzb.musicplayer.fragment.ArtistFragment;
import com.lzb.musicplayer.fragment.HomeFragment;
import com.lzb.musicplayer.fragment.PlaylistFragment;
import com.lzb.musicplayer.fragment.SongFragment;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class HomeActivity extends BaseTabFragmentHomeActivity<ActivityHomeBinding> implements AudioPlayerManager.PlaybackStateListener {

    private AudioPlayerManager playerManager;

    @Override
    protected void initView() {
        super.initView();
        playerManager = AudioPlayerManager.getInstance(this);
        playerManager.addListener(this);

        mDataBinding.llHomeBottom.setOnClickListener(null);
    }

    @NonNull
    @Override
    protected List<BaseTabFragmentHomeActivity<ActivityHomeBinding>.FragmentViewBinder> getFragmentViewBinders() {
        List<FragmentViewBinder> listFragment = new ArrayList<>();
        listFragment.add(new FragmentViewBinder(HomeFragment.class, R.id.llHome));
        listFragment.add(new FragmentViewBinder(SongFragment.class, R.id.llSong));
        listFragment.add(new FragmentViewBinder(AlbumFragment.class, R.id.llAlbum));
        listFragment.add(new FragmentViewBinder(ArtistFragment.class, R.id.llArtist));
        listFragment.add(new FragmentViewBinder(PlaylistFragment.class, R.id.llPlaylist));
        return listFragment;
    }

    @Override
    protected void onFragmentViewClick(View view) {
        int id = view.getId();
        clearSelection();
        if (id == R.id.llHome) {
            mDataBinding.ivHome.setImageResource(R.drawable.shouye_on);
            mDataBinding.tvHome.setTextColor(Color.parseColor("#FFD400"));
        } else if (id == R.id.llSong) {
            mDataBinding.ivSong.setImageResource(R.drawable.gechu_on);
            mDataBinding.tvSong.setTextColor(Color.parseColor("#FFD400"));
        } else if (id == R.id.llAlbum) {
            mDataBinding.ivAlbum.setImageResource(R.drawable.zhuanji_on);
            mDataBinding.tvAlbum.setTextColor(Color.parseColor("#FFD400"));
        } else if (id == R.id.llArtist) {
            mDataBinding.ivArtist.setImageResource(R.drawable.yishujia_on);
            mDataBinding.tvArtist.setTextColor(Color.parseColor("#FFD400"));
        } else if (id == R.id.llPlaylist) {
            mDataBinding.ivPlaylist.setImageResource(R.drawable.bofangliebiao_on);
            mDataBinding.tvPlaylist.setTextColor(Color.parseColor("#FFD400"));
        }
    }

    @Override
    protected int getFragmentContainerId() {
        return R.id.flContainer;
    }

    @Override
    protected int onCreate() {
        return R.layout.activity_home;
    }

    public void setFragmentMusicList(List<MusicFile> listFile) {
        if (listFile != null && !listFile.isEmpty()) {
            mDataBinding.audioPlayerView.setPlayList(listFile, 0);
            mDataBinding.audioPlayerView.startPlay();
        }
    }

    public void setRandomMusicList(List<MusicFile> listFile) {
        if (listFile != null && !listFile.isEmpty()) {
            int pos = new Random().nextInt(listFile.size());
            mDataBinding.audioPlayerView.setPlayList(listFile, pos);
            mDataBinding.audioPlayerView.setSj();
            mDataBinding.audioPlayerView.startPlay();
        }
    }

    public void setNextMusic(List<MusicFile> listFile) {
        if (listFile != null && listFile.size() > 1) {
            mDataBinding.audioPlayerView.setPlayList(listFile, 0);
            mDataBinding.audioPlayerView.next();
            mDataBinding.audioPlayerView.startPlay();
        }
    }

    private void clearSelection() {
        mDataBinding.ivHome.setImageResource(R.drawable.shouye_off);
        mDataBinding.tvHome.setTextColor(Color.parseColor("#A6FFFFFF"));
        mDataBinding.ivSong.setImageResource(R.drawable.gechu_off);
        mDataBinding.tvSong.setTextColor(Color.parseColor("#A6FFFFFF"));
        mDataBinding.ivAlbum.setImageResource(R.drawable.zhuanji_off);
        mDataBinding.tvAlbum.setTextColor(Color.parseColor("#A6FFFFFF"));
        mDataBinding.ivArtist.setImageResource(R.drawable.yishujia_off);
        mDataBinding.tvArtist.setTextColor(Color.parseColor("#A6FFFFFF"));
        mDataBinding.ivPlaylist.setImageResource(R.drawable.bofangliebiao_off);
        mDataBinding.tvPlaylist.setTextColor(Color.parseColor("#A6FFFFFF"));
    }

    @Override
    public void onPlaybackStateChanged(boolean isPlaying) {
        if (isPlaying) {
            mDataBinding.audioPlayerView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onPlaybackStateChanged(int state) {

    }

    @Override
    public void onProgressUpdated(long position, long duration) {

    }

    @Override
    public void onTrackChanged(MusicFile currentTrack) {

    }
}